package com.hilaver.dzmis.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.product.ProductDZColor;
import com.hilaver.dzmis.product.ProductIdentification;
import com.hilaver.dzmis.product.ProductLaunching;
import com.hilaver.dzmis.service.AbstractBaseService;
import com.hilaver.dzmis.util.SimpleObj2XML;

public class ProductLaunchingServiceImpl extends AbstractBaseService {
	
	private ProductDZColorServiceImpl productDZColorService;
	public ProductLaunchingServiceImpl(){
		this.productDZColorService = new ProductDZColorServiceImpl();
	}
	
	public String edit(ProductLaunching obj) throws Exception {
		String operation = null;
		if(obj.getId() != null){
			operation = "02";
		}else{
			operation = "01";
		}
		
		if(obj.getIsStock() == null){
			obj.setIsStock("02");
		}
		this.dao.saveOrUpdate(obj);
		super.editSysMbox(obj, operation);
		return "{success: true}";
	}

	@Override
	public String delete(int id) throws Exception {
		super.delete(ProductLaunching.class.getName(), id);
		return super.delete(ProductLaunching.class.getName(), id);
	}

	@Override
	public String get(int id) throws Exception {
		ProductLaunching oz = (ProductLaunching)this.dao.get(ProductLaunching.class.getName(), id);
		
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		
		xmlSB.append(SimpleObj2XML.toXMLFragment(oz));
		
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getWithDescription(int id) throws Exception {
		ProductLaunching pl = (ProductLaunching)this.dao.get(ProductLaunching.class.getName(), id);
		
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		xmlSB.append(SimpleObj2XML.toXMLFragment(pl));
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	@Override
	public String getAllPagination(int page, String sort, String order)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	public String getAllPagination(int page, String sort,
			String order, String[] filters) throws Exception {
		int offset = Constants.PAGE_SIZE * (page - 1);
		List objList = this.dao.getAllPagination(ProductLaunching.class
				.getName(), offset, Constants.PAGE_SIZE, sort, order, filters);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (Object obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);

			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));

			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(ProductLaunching.class.getName(),
				filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	
	public String getAll(Integer piId) throws Exception{
		String [] filters = new String[1];
		filters[0] = "productIdentification.id = " + piId;
		List objList = this.dao.getAll(ProductLaunching.class.getName(), "id", "asc", filters);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		int offset = 0;
		for (Object obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			
			ProductLaunching pl = (ProductLaunching)obj;
			
			String variant = pl.getVariant();
			String color = "";
			Set<ProductDZColor> colorSet = pl.getProductIdentification().getProductDZColors();
			for(ProductDZColor piDZColor: colorSet){
				if(variant.equals(piDZColor.getVariant())){
					color += piDZColor.getBiDZColor().getCustomerColorCode() + "&nbsp;&nbsp;" + piDZColor.getPercentage() + "%&nbsp;&nbsp;&nbsp;&nbsp;";
				}
			}
			
			xmlSB.append("<color>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(color);
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</color>");
			
			
			if(pl.getBiFactory() != null){
				xmlSB.append("<biFactoryId>");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append(pl.getBiFactory().getId());
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</biFactoryId>");
				
				xmlSB.append("<factoryNameCN>");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append(pl.getBiFactory().getNameCN());
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</factoryNameCN>");
				
				xmlSB.append("<factoryNameEN>");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append(pl.getBiFactory().getNameCN());
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</factoryNameEN>");
			}else{
				xmlSB.append("<biFactoryId>");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append("");
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</biFactoryId>");
				
				xmlSB.append("<factoryNameCN>");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append("");
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</factoryNameCN>");
				
				xmlSB.append("<factoryNameEN>");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append("");
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</factoryNameEN>");
			}
			
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}
		
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append("1");
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(ProductLaunching.class.getName(),filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getAllVariant(Integer id) throws Exception {
		ProductLaunching pl = (ProductLaunching)this.dao.get(ProductLaunching.class.getName(), id);
		Set<ProductDZColor> productDZColors = pl.getProductIdentification().getProductDZColors();
		
		List<ProductDZColor> pDZColorList = new ArrayList<ProductDZColor>();
		for (Iterator<ProductDZColor> iterator = productDZColors.iterator(); iterator.hasNext();) {
			ProductDZColor i =  iterator.next();
			if(i.getVariant().equals(pl.getVariant())){
				pDZColorList.add(i);
			}
		}
		
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		int offset = 0;
		this.productDZColorService.setRealFileDir(realFileDir);
		for(ProductDZColor pDZColor : pDZColorList){
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			
			xmlSB.append(SimpleObj2XML.toXMLFragment(pDZColor));
			xmlSB.append(this.productDZColorService.toXMLFragment(pDZColor));
			
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append("1");
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(pDZColorList.size());
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String addToStock(Integer id, String isStock) throws Exception {
		ProductLaunching pl = (ProductLaunching)this.dao.get(ProductLaunching.class.getName(), id);
		pl.setIsStock(isStock);
		this.dao.saveOrUpdate(pl);
		return "success";
	}
	
	public String validate(ProductLaunching obj) throws Exception{
		ProductLaunching pl = (ProductLaunching) this.dao.get(ProductLaunching.class.getName(), obj.getId());
		pl.setScheduleDate(obj.getScheduleDate());
		pl.setStockDate(obj.getStockDate());
		pl.setQuantity(obj.getQuantity());
		pl.setQuantityConsistent(obj.getQuantityConsistent());
		pl.setQuantityNotConsistent(obj.getQuantityNotConsistent());
		pl.setBiFactory(obj.getBiFactory());
		if(pl.getStockDate() != null){
			pl.setIsStock("01");
		}else{
			pl.setIsStock("02");
		}
		
		boolean isInprocess1 = false;
		Set pls = pl.getProductIdentification().getProductLaunchings();
		for (Iterator iterator = pls.iterator(); iterator.hasNext();) {
			ProductLaunching object = (ProductLaunching) iterator.next();
			if("02".equals(object.getIsStock()) || object.getIsStock() == null){
				isInprocess1 = true;
				break;
			}
		}
		ProductIdentification pi = (ProductIdentification)this.dao.get(ProductIdentification.class.getName(), pl.getProductIdentification().getId());
		if(isInprocess1){
			pi.setIsInprocess1("01");
		}else{
			pi.setIsInprocess1("02");
		}
		this.dao.saveOrUpdate(pl);
		this.dao.saveOrUpdate(pi);
		return "{success: true}";
	}
	
	public static void main(String[] args) throws Exception{
		ProductLaunchingServiceImpl os = new ProductLaunchingServiceImpl();
		System.out.println(os.getAll(2));
		//char index = 'C';
		//System.out.println(++index);
	}

}
